package com.example.redisstudy.concurrent;

public class ReentrantLockExample {

    // synchronized 方法，模拟外层方法
    public synchronized void outerMethod() {
        System.out.println(Thread.currentThread().getName() + " entered outerMethod");

        // 调用内层方法，这里会再次获取到同一个锁
        innerMethod();

        System.out.println(Thread.currentThread().getName() + " exited outerMethod");
    }

    // synchronized 方法，模拟内层方法
    public synchronized void innerMethod() {
        System.out.println(Thread.currentThread().getName() + " entered innerMethod");

        // 模拟一些处理
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        System.out.println(Thread.currentThread().getName() + " exited innerMethod");
    }

    public static void main(String[] args) {
        ReentrantLockExample example = new ReentrantLockExample();

        // 创建线程测试可重入性
        Thread thread1 = new Thread(example::outerMethod, "Thread-1");
        Thread thread2 = new Thread(example::outerMethod, "Thread-2");

        thread1.start();
        thread2.start();
    }
}
